package com.github.thinwonton.seckill.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.thinwonton.seckill.entity.po.Seckill;
import com.github.thinwonton.seckill.entity.web.AjaxResult;
import com.github.thinwonton.seckill.entity.web.Exposer;
import com.github.thinwonton.seckill.entity.web.SeckillExecution;
import com.github.thinwonton.seckill.exception.SeckillClosedException;
import com.github.thinwonton.seckill.exception.SeckillRepeateException;
import com.github.thinwonton.seckill.service.SeckillService;

/**
 * 秒杀模块控制器
 * 
 * @author hugo
 *
 */
@Controller
@RequestMapping(value = "/seckill")
public class SecKillController {

	private Logger logger = LoggerFactory.getLogger(SecKillController.class);

	@Autowired
	private SeckillService seckillService;

	/*
	 * 获取秒杀列表页面
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<Seckill> seckillList = seckillService.getSeckillList();
		model.addAttribute("seckillList", seckillList);
		return "list";
	}

	/**
	 * 获取秒杀详情页面
	 * 
	 * @param seckillId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{seckillId}/detail", method = RequestMethod.GET)
	public String detail(@PathVariable(value = "seckillId") Long seckillId, Model model) {
		if (seckillId == null) {
			return "redirect:/seckill/list";
		}

		Seckill seckill = seckillService.getById(seckillId);
		if (seckill == null) {
			return "redirect:/seckill/list";
		}
		model.addAttribute("seckill", seckill);
		return "detail";
	}

	/**
	 * 执行秒杀
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{seckillId}/execute", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult<SeckillExecution> execute(@PathVariable(value = "seckillId") Long seckillId,
			@RequestParam(value = "secretKey", required = true) String secretKey,
			@RequestParam(value = "userid", required = true) Long userid) {

		if (seckillId <= 0 || "".equals(secretKey) || userid <= 0) {
			return new AjaxResult<>(false, "非法参数");
		}

		try {
			SeckillExecution seckillExecution = seckillService.execute(seckillId, secretKey, userid);
			return new AjaxResult<SeckillExecution>(true, seckillExecution);
		} catch (SeckillClosedException e1) {
			return new AjaxResult<SeckillExecution>(true, new SeckillExecution(false, e1.getMessage()));
		} catch (SeckillRepeateException e2) {
			return new AjaxResult<SeckillExecution>(true, new SeckillExecution(false, e2.getMessage()));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new AjaxResult<SeckillExecution>(true, "服务器内部错误，请稍后重试");
		}
	}

	/**
	 * 暴露秒杀URL
	 * 
	 * @param seckillId
	 * @return
	 */
	@RequestMapping(value = "/{seckillId}/expose", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult<Exposer> expose(@PathVariable(value = "seckillId") Long seckillId) {
		if (seckillId == null || seckillId <= 0) {
			return new AjaxResult<>(false, "秒杀商品不存在");
		}

		Exposer exposer = seckillService.expose(seckillId);
		if (exposer == null) {
			return new AjaxResult<>(false, "秒杀商品不存在");
		}
		return new AjaxResult<Exposer>(true, exposer);
	}
}
